/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mvc_template_app.controller;

import com.mvc_template_app.model.LoginForm;
import com.mvc_template_app.service.TemplateAppService;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

/**
 *
 * @author gorjan ilievski
 */
@Controller
@SessionAttributes({"loginForm"})
public class LoginController {

    @Autowired
    private TemplateAppService templateAppService;

    public void setTemplateAppService(TemplateAppService templateAppService) {
        this.templateAppService = templateAppService;
    }

    @ModelAttribute
    public LoginForm getLoginForm(HttpSession session) throws InstantiationException, IllegalAccessException {
        return (LoginForm) getSessionForm(session, LoginForm.class);
    }

    protected Object getSessionForm(HttpSession session, Class<?> formClass) {
        String attrName = formClass.getSimpleName().substring(0, 1).toLowerCase() + formClass.getSimpleName().substring(1);
        Object form = session.getAttribute(attrName);
        if (form == null) {
            try {
                form = formClass.newInstance();
            } catch (InstantiationException e) {
                throw new RuntimeException(e);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
            session.setAttribute(attrName, form);
        }
        return form;
    }

    @RequestMapping(value = "/login*", method = RequestMethod.POST)
    public String submitLogin(
            HttpServletRequest request,
            HttpServletResponse response,
            @ModelAttribute("loginForm") LoginForm loginMMB,
            @RequestParam String action,
            ModelMap model) {
        HttpSession session = request.getSession();

        if (action != null) {
            model.addAttribute("action", action);
            if (action.equals("new")) {
                model.addAttribute("loginForm", new LoginForm());
            } else if (action.equals("search")) {
                List<LoginForm> loginForms = templateAppService.findByUsername(loginMMB.getUsername());
                model.addAttribute("searchResult", loginForms);
                session.setAttribute("searchResult", loginForms);
                return "login";
            } else if (action.equals("save")) {
                templateAppService.saveLoginObject(loginMMB);
            }
        }
        return "redirect:login.htm";
    }

    @RequestMapping(value = "/login*", method = RequestMethod.GET)
    public String doLogin(
            HttpServletRequest req,
            @RequestParam(value = "editIndex", required = false) Integer editIndex,
            @RequestParam(value = "removeIndex", required = false) Integer removeIndex,
            ModelMap model) {
        HttpSession session = req.getSession();

        if (editIndex != null) {
            List<LoginForm> loginForms = (List<LoginForm>) session.getAttribute("searchResult");
            model.addAttribute("loginForm", loginForms.get(editIndex));
        }
        if (removeIndex != null) {
            List<LoginForm> loginForms = (List<LoginForm>) session.getAttribute("searchResult");
            templateAppService.deleteLoginObject(loginForms.get(removeIndex));
            loginForms.remove(loginForms.get(removeIndex));
            model.addAttribute("searchResult", loginForms);
            session.setAttribute("searchResult", loginForms);
        }
        return "login";
    }
}
